const express = require('express')
const ejs = require('ejs')
const path = require('path')

const app = express()

const {renderPage} = require("./src/utills/renderPage")
const {readFile} = require("./src/utills/file-utils")


//配置ejs视图的目录
app.set("views",  path.join(__dirname, 'views'));    //    '/views代表存放视图的目录'
//启动视图引擎，并指定模板文件文件类型：ejs
app.set('view engine', 'ejs')
//模板类型指定为html
app.engine('html',ejs.__express)
//启动视图引擎
app.set('view engine','html')


//设置静态目录
app.use(express.static("public"))


app.get("/error",(req,res) =>{
    res.send("hello world !")
})


app.get("/home",(req,res) =>{
    // res.write('<link rel="stylesheet" href="/public/css/style.css"/>')
    // const pages = renderPage(data)
    readFile("public/json/data.json").then(resData => {
        const {code, message, data} = JSON.parse(resData)
        res.render("index",{users:data},(error,html) =>{
            res.end(html)
        })
    })
})


const server = app.listen(8081,"127.0.0.1",()=>{
    const host = server.address().address
    const port = server.address().port
    console.log("host:",host)
    console.log(`server run on ${host}:${port}`)
})
